﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.IoC;
using LinFu.Reflection;

namespace CoreEx.Common.Implementation.Ioc
{
    /// <summary>
    /// Lists files in a given directory that should be weaved before loaded into the container.
    /// </summary>
    [Implements(typeof(IDirectoryListing),ServiceName = "WeaveTargetDirectoryLister")]
    public class WeavedDirectoryLister : IDirectoryListing,IInitialize
    {
        private IDictionary<string, IList<Type>> _assemblyWeavers;      
        
        public IEnumerable<string> GetFiles(string path, string searchPattern)
        {            
            var files = Directory.GetFiles(path, searchPattern);
            return files.Where(f => _assemblyWeavers.ContainsKey(Path.GetFileName(f)));
        }
      

        public void Initialize(IServiceContainer source)
        {
            _assemblyWeavers = source.GetService<IDictionary<string, IList<Type>>>("AssemblyWeavers");            
        }
    }
}